package com.eagle.demo.controller;

import com.eagle.core.annotation.SelfAutowired;
import com.eagle.core.annotation.SelfController;
import com.eagle.core.annotation.SelfRequestMapping;
import com.eagle.core.annotation.SelfRequestParam;
import com.eagle.core.bean.SelfResponse;
import com.eagle.core.bean.View;
import com.eagle.demo.bean.UserInfo;
import com.eagle.demo.service.DemoService;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@SelfController
@SelfRequestMapping("/demo")
public class DemoController {
	@SelfAutowired private DemoService demoService;

//	http://localhost:8081/demo/query?name=eagle
	@SelfRequestMapping("/query")
	public SelfResponse query(HttpServletRequest req, HttpServletResponse resp, @SelfRequestParam("name") String name){
		UserInfo userInfo = demoService.get(name);
		SelfResponse response = new SelfResponse(userInfo);
		return response;
	}

//	http://localhost:8081/demo/page
	@SelfRequestMapping("/page")
	public View page(HttpServletRequest req, HttpServletResponse resp){
		List<UserInfo> userList = demoService.findAllUser();
		return new View("index.jsp").addModel("userList", userList);
	}


//	http://localhost:8081/demo/add?a=1&b=2
	@SelfRequestMapping("/add")
	public void add(HttpServletRequest req, HttpServletResponse resp, @SelfRequestParam("a") Integer a, @SelfRequestParam("b") Integer b){
		try {
			resp.getWriter().write(a+" + "+b+" = "+(a+b));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SelfRequestMapping("/remove")
	public void remove(HttpServletRequest req, HttpServletResponse resp, @SelfRequestParam("id") Integer id){

	}
}
